package com.neusoft.elmboot.controller;

import com.neusoft.elmboot.po.PointsRedemption;
import com.neusoft.elmboot.po.PointsStoreItem;
import com.neusoft.elmboot.service.PointsService;
import com.neusoft.elmboot.service.PointsStoreService;
import com.neusoft.elmboot.util.response.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/elm/PointsStoreController")
public class PointsStoreController {
	
	@Autowired
	private PointsStoreService pointsStoreService;
	
	@Autowired
	private PointsService pointsService;
	
	@RequestMapping("/items")
	public ResultBody getAllItems() {
		try {
			List<PointsStoreItem> items = pointsStoreService.getAllAvailableItems();
			return ResultBody.success("获取商品列表成功", items);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultBody.error("获取商品列表失败：" + e.getMessage(), null);
		}
	}
	
	// ==================== 管理员功能：积分商品管理 ====================
	
	/**
	 * 管理员：获取所有积分商品列表
	 */
	@PostMapping("/list")
	public ResultBody listAllItems() {
		try {
			List<PointsStoreItem> items = pointsStoreService.getAllItems();
			return ResultBody.success("获取商品列表成功", items);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultBody.error("获取商品列表失败：" + e.getMessage(), null);
		}
	}
	
	/**
	 * 管理员：新增积分商品
	 */
	@PostMapping("/add")
	public ResultBody addItem(@RequestBody PointsStoreItem item) {
		try {
			pointsStoreService.addItem(item);
			return ResultBody.success("新增商品成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultBody.error("新增商品失败：" + e.getMessage(), null);
		}
	}
	
	/**
	 * 管理员：更新积分商品
	 */
	@PostMapping("/update")
	public ResultBody updateItem(@RequestBody PointsStoreItem item) {
		try {
			pointsStoreService.updateItem(item);
			return ResultBody.success("更新商品成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultBody.error("更新商品失败：" + e.getMessage(), null);
		}
	}
	
	/**
	 * 管理员：更新积分商品状态
	 */
	@PostMapping("/updateStatus")
	public ResultBody updateItemStatus(@RequestBody PointsStoreItem item) {
		try {
			if (item.getItemId() == null || item.getStatus() == null) {
				return ResultBody.error("商品ID或状态不能为空", null);
			}
			pointsStoreService.updateItemStatus(item.getItemId(), item.getStatus());
			return ResultBody.success("更新商品状态成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultBody.error("更新商品状态失败：" + e.getMessage(), null);
		}
	}
	
	@RequestMapping("/redeem")
	public ResultBody redeemItem(@RequestParam String userId, @RequestParam Integer itemId) {
		try {
			boolean success = pointsStoreService.redeemItem(userId, itemId);
			if (success) {
				return ResultBody.success("兑换成功", null);
			} else {
				return ResultBody.error("兑换失败：积分不足或商品库存不足", null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultBody.error("兑换失败：" + e.getMessage(), null);
		}
	}
	
	@RequestMapping("/redemptions")
	public ResultBody getRedemptions(@RequestParam String userId) {
		try {
			List<PointsRedemption> redemptions = pointsStoreService.getRedemptionsByUserId(userId);
			return ResultBody.success("获取兑换记录成功", redemptions);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultBody.error("获取兑换记录失败：" + e.getMessage(), null);
		}
	}
	
	@RequestMapping("/user/points")
	public ResultBody getUserPoints(@RequestParam String userId) {
		try {
			com.neusoft.elmboot.po.User user = pointsService.getUserPointsInfo(userId);
			if (user == null) {
				return ResultBody.error("用户不存在", null);
			}
			return ResultBody.success("获取积分信息成功", user);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultBody.error("获取积分信息失败：" + e.getMessage(), null);
		}
	}
}

